//
//  RBCompeExpListController.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBCompeExpListController.h"
#import "RBCompeExpModel.h"
#import "RBCompeExpZsModel.h"
#import "RBCompeExpZsListCell.h"

@interface RBCompeExpListController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)NSMutableArray *dataArray;
@end

@implementation RBCompeExpListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.view.backgroundColor = RBColor(245, 245, 245);
    self.articleFinish = @"";
    [self rb_initTableViewUI];
    [self rb_initRefresh];
}
-(void)rb_initTableViewUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
        [self.tableView registerClass:[RBCompeExpZsListCell class] forCellReuseIdentifier:NSStringFromClass([RBCompeExpZsListCell class])];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.aimView = self.tableView;
    self.emptyDataImageName = @"qsy_zwxl";
    self.emptyDataText = @"暂无数据";
    [self.tableView.mj_header beginRefreshing];
    self.aimView.mj_footer.hidden = YES;
}
-(void)rb_initRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(rb_loadQueryMatchZsData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
}
-(void)rb_loadQueryMatchZsData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"sportId"] = self.sportId;
    dict[@"matchId"] = self.matchId;
    MJWeakSelf
    [RBNetworkTool GET:RBQueryMatchZsURL parameters:dict success:^(id responseObject) {
        if ([responseObject[@"success"] boolValue]) {
            [self.dataArray removeAllObjects];
            
            NSMutableArray *dataArray = [RBCompeExpModel mj_objectArrayWithKeyValuesArray:RBSafeArray(responseObject, @"result")];
            NSMutableArray *keyVluesArr = [NSMutableArray arrayWithCapacity:0];
            BOOL isjq = NO; // 是否有进球的类型
            
            for (RBCompeExpModel *compExpModel in dataArray) {
                
                if (compExpModel.forecastResult == nil) {
                    compExpModel.forecastResult = @"";
                }
                // 大小分
                if ([compExpModel.forecastType isEqualToString:@"dxf"]) {
                    NSArray *keyArray = @[@"sf1",@"sf2",@"sf3"];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        NSMutableArray *ResultArray = [compExpModel.forecastResult componentsSeparatedByString:@","].mutableCopy;
                        if (compExpModel.forecastResult.length > 0) {
                            [ResultArray exchangeObjectAtIndex:1 withObjectAtIndex:0];
                        }
                        NSDictionary *keyDic = @{@"dxf":zqJqDic,
                                                 @"forecastType":@"dxf",
                                                 @"forecastResult":[ResultArray componentsJoinedByString:@","],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"6"
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                // 让分胜负
                if ([compExpModel.forecastType isEqualToString:@"rfsf"]){
                    NSArray *keyArray = @[@"sf0",@"sf1",@"sf2"];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        //
                        NSDictionary *keyDic = @{@"rfsf":zqJqDic,
                                                 @"forecastType":@"rfsf",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"5"
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                // 让球胜负
                if ([compExpModel.forecastType isEqualToString:@"rqsf"]) {
                    NSArray *keyArray = @[@"sf0",@"sf1",@"sf2"];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        NSDictionary *keyDic = @{@"rqsf":zqJqDic,
                                                 @"forecastType":@"rqsf",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"4"
                                                 
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                // 让球胜平负
                if ([compExpModel.forecastType isEqualToString:@"rqspf"]) {
                    NSArray *keyArray = @[@"sf0",@"sf1",@"sf2",@"sf3"];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        //
                        NSDictionary *keyDic = @{@"rqspf":zqJqDic,
                                                 @"forecastType":@"rqspf",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"3"
                        };
                        [keyVluesArr addObject:keyDic];
                        
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                //胜平负
                if ([compExpModel.forecastType isEqualToString:@"spf"]) {
                    NSArray *keyArray = @[@"sf3",@"sf1",@"sf0"];
                    compExpModel.zs = [NSString stringWithFormat:@"%@", compExpModel.zs];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        NSDictionary *keyDic = @{@"spf":zqJqDic,
                                                 @"forecastType":@"spf",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"2"
                                                 
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                // 大小球
                if ([compExpModel.forecastType isEqualToString:@"dxq"]) {
                    NSArray *keyArray = @[@"sf1",@"sf2",@"sf3"];
                    NSArray *valueArray = [compExpModel.zs componentsSeparatedByString:@","];
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        NSDictionary *keyDic = @{@"dxq":zqJqDic,
                                                 @"forecastType":@"dxq",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"1"
                                                 
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
                
                if ([compExpModel.forecastType isEqualToString:@"jq"]) {
                    isjq = YES;
                    NSArray *keyArray = @[@"j0",@"j1",@"j2",@"j3",@"j4",@"j5",@"j6",@"j7"];
                    compExpModel.zs = [NSString stringWithFormat:@"%@", compExpModel.zs];
                    NSArray *valueArray =  [compExpModel.zs componentsSeparatedByString:@","];
                    
                    NSMutableDictionary *zqJqDic = [NSMutableDictionary dictionaryWithCapacity:0];
                    if (keyArray.count == valueArray.count) {
                        for (int i = 0; i<valueArray.count; i++) {
                            // 将字典转换成数组,进行组装
                            [zqJqDic setObject:valueArray[i] forKey:keyArray[i]];
                        }
                        NSDictionary *keyDic = @{@"jq":zqJqDic,
                                                 @"forecastType":@"jq",
                                                 @"forecastResult":[compExpModel.forecastResult rb_editNullStr],
                                                 @"matchResult":[self replaceStrnill:compExpModel.matchResult],
                                                 @"result":[self replaceStrnill:compExpModel.result],
                                                 @"articleFinish":self.articleFinish,
                                                 @"midZS":compExpModel.zs,
                                                 @"indexTag":@"0"
                        };
                        [keyVluesArr addObject:keyDic];
                    }else{
                        [MBProgressHUD rb_showAutoMessage:@"总进球数据异常" ToView:RBWindow];
                    }
                }
            }
            
            NSMutableArray *modelArray = [RBCompeExpZsModel mj_objectArrayWithKeyValuesArray:keyVluesArr];
            NSSortDescriptor *totalDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"indexTag" ascending:YES];
            NSArray *desptorArr = @[totalDescriptor];
            NSMutableArray *sortArr = [NSMutableArray arrayWithCapacity:0];
            sortArr = [modelArray.mutableCopy sortedArrayUsingDescriptors:desptorArr].mutableCopy;
            [self.dataArray addObjectsFromArray:sortArr];
            
            if (dataArray.count > 0) {
                weakSelf.loadingState = LoadingStateNormal;
            }else{
                weakSelf.loadingState = LoadingStateNoData;
            }
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
            
        }
    } error:^(id responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.loadingState = LoadingStateUnKnownError;
    } failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.loadingState = LoadingStateNoNetwork;
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    self.tableView.mj_footer.hidden = (self.dataArray.count == 0);
    return  self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RBCompeExpZsModel *model = self.dataArray[indexPath.section];
    RBCompeExpZsListCell *compeExpZsListCell = [tableView dequeueReusableCellWithIdentifier:@"RBCompeExpZsListCell" forIndexPath:indexPath];

    compeExpZsListCell.forecastResultArray = [model.forecastResult componentsSeparatedByString:@","].mutableCopy;
    if ([model.forecastType isEqualToString:@"jq"]||[model.forecastType isEqualToString:@"rqspf"]) {
        compeExpZsListCell.widthCount = 4;
    }else if ([model.forecastType isEqualToString:@"spf"] ||[model.forecastType isEqualToString:@"rqsf"] || [model.forecastType isEqualToString:@"dxq"]|| [model.forecastType isEqualToString:@"dxf"]|| [model.forecastType isEqualToString:@"rfsf"]){
        compeExpZsListCell.widthCount = 3;
    }
    compeExpZsListCell.model = model;
    return compeExpZsListCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RBCompeExpZsModel *model = self.dataArray[indexPath.section];
    if ([model.forecastType isEqualToString:@"jq"]) {
        return RBScaleWidth(175);
    }else if ([model.forecastType isEqualToString:@"spf"]||[model.forecastType isEqualToString:@"rqsf"]||[model.forecastType isEqualToString:@"dxq"]||[model.forecastType isEqualToString:@"rqspf"]||[model.forecastType isEqualToString:@"dxf"]||[model.forecastType isEqualToString:@"rfsf"]){
        return RBScaleWidth(113);
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(10))];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return RBScaleWidth(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, RBScreenWidth, 0.01)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(NSString *)replaceStrnill:(NSString *)str{
    if (str == nil) {
        return @"";
    }else{
        return str;
    }
}
- (NSMutableArray *)dataArray{
    if (_dataArray==nil) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
@end
